﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Globalization;
using System.Threading;
using System.Security.Cryptography;

namespace GolfSigma.Inventory.Extension
{
    public static class StringExtension
    {
        public static readonly string DateTimeFormatShort = "yyyy-MM-ddTHH:mm:ss";
        public static readonly string DateTimeFormatUTC = "yyyy-MM-ddTHH:mm:ssZ";

        public static string Prefix(string data, string prefix)
        {
            return prefix + data;
        }

        public static string Prefix(string data, string prefix, int? index)
        {
            string i = index.HasValue ? (index.Value.ToString() + ".") : "";
            return string.Concat(
                prefix,
                i,
                data);
        }

        /// <summary>
        /// true, if the string can be parse as Double respective Int32
        /// Spaces are not considred.
        /// </summary>
        /// <param name="s">input string</param>
        /// <param name="floatpoint">true, if Double is considered,
        /// otherwhise Int32 is considered.</param>
        /// <returns>true, if the string contains only digits or float-point</returns>
        public static bool IsNumber(string s, bool floatpoint)
        {
            int i;
            double d;
            string withoutWhiteSpace = StringExtension.RemoveSpaces(s);
            if (floatpoint)
                return double.TryParse(withoutWhiteSpace, NumberStyles.Any,
                    Thread.CurrentThread.CurrentUICulture, out d);
            else
                return int.TryParse(withoutWhiteSpace, out i);
        }

        /// <summary>
        /// remove white space, not line end
        /// Useful when parsing user input such phone,
        /// price int.Parse("1 000 000".RemoveSpaces(),.....
        /// </summary>
        /// <param name="s"></param>
        /// <param name="value">string without spaces</param>
        public static string RemoveSpaces(string s)
        {
            return s.Replace(" ", "");
        }
    }
}
